<!--
@description ：风控数据
-->

<template>
  <div>
    <NormalForm :enumOpt="enumOpt" :formList="formList" :operateForm="queryForm"
                pageTitle=""
                @handleQuery="handleQuery" @handleReset="handleReset"></NormalForm>
    <el-card style="margin-top: 20px;">
      <el-radio-group v-model="queryForm.timeType" style="display: flex;justify-content: space-between;width: 360px"
                      @input="radioDateSelect(queryForm.timeType)">
        <div v-for="item in timeTypeList" :key="item.dictValue">
          <el-radio-button :label="item.dictValue">{{ item.dictLabel }}</el-radio-button>
        </div>
      </el-radio-group>
      <div style="margin-top: 20px">
        <el-table v-loading="loading" :data="tableList" max-height="560" min-height="420" style="width: 100%">
          <el-table-column label="序号" min-width="80" prop="id" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="false" align="center" label="触发员工" min-width="140"
                           prop="weUserName">
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" align="center" label="触发规则" min-width="180"
                           prop="ruleId">
            <template slot-scope="{ row }">
              {{ row.ruleId ? riskTypeList.filter(item => item.dictValue == row.ruleId)[0].dictLabel : '' }}
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="false" align="center" label="触发时间" min-width="170"
                           prop="triggerTime">
          </el-table-column>
          <el-table-column :show-overflow-tooltip="false" align="center" label="规则上限" min-width="120"
                           prop="ruleLimit">
          </el-table-column>
          <el-table-column :show-overflow-tooltip="false" align="center" label="规则更新时间" min-width="170"
                           prop="updateTime"></el-table-column>
          <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作"
                           width="140">
            <template slot-scope="{ row }">
              <div style="display: flex; justify-content: space-around">
                <el-button size="mini" type="text" @click="editItem(row)" v-has-permi="['risk:control:edit']">编辑</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <NormalPagination :currentPage="queryForm.pageNum" :page-size-list="pageSizeList" :pageSize="queryForm.pageSize"
                          :total="total" @handleCurrentChange="handleCurrentChange"
                          @handleSizeChange="handleSizeChange"></NormalPagination>
      </div>
    </el-card>
    <el-dialog :visible.sync="editVisible" title="风控数据编辑" width="550px">
      <div style="margin-left: 22px">员工: {{ editForm.weUserName }}</div>
      <el-form ref="editForm" :model="editForm" :rules="editRules" label-position="left" label-suffix=":"
               label-width="190px">
        <el-form-item :label="editForm.label" prop="ruleLimit">
          <el-input-number v-model="editForm.ruleLimit" :min="1" :placeholder="`请输入${editForm.label}`"
                           :precision="0"
                           :step="1" controls-position="right"
                           style="width: 220px;">
          </el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
            <el-button :loading="editLoading" size="mini" @click="editVisible = false">取 消</el-button>
            <el-button :loading="editLoading" size="mini" type="primary" @click="editSubmit">确 定</el-button>
          </span>
    </el-dialog>
  </div>
</template>
<script>
import * as api from "@/api/organization";
import * as $api from '@/api/riskManager/data'

export default {
  name: "riskManagerData",
  //页面名称，必填
  props: {},
  components: {},
  data() {
    return {
      queryForm: {
        weUserIdList: [],
        dateRange: [],
        ruleIdList: [],
        timeType: '0',
        pageNum: 1,
        pageSize: 10,
      },
      formList: [
        {
          label: '触发员工',
          value: [],
          prop: 'weUserIdList',
          type: 'SELECT',
          emunName: 'weUserIdList',
          multiple: true,
          defaultNoLabel: true,
        },
        {
          label: '触发规则',
          value: '',
          prop: 'ruleIdList',
          type: 'SELECT',
          emunName: 'ruleList',
          defaultNoLabel: true,
          multiple: true,
        },
        {
          label: '',
          value: [],
          prop: 'dateRange',
          type: 'DATE_RANGE',
          formate: 'yyyy-MM-dd',
          emunName: '',
          xs: 24,
          ls: 12,
          md: 10,
          lg: 8,
          xl: 6,
        },
      ], // 查询框
      enumOpt: {
        weUserIdList: [],
        ruleList: [],
      },
      timeTypeList: [
        {dictValue: '0', dictLabel: '全部'},
        {dictValue: '1', dictLabel: '最近7天'},
        {dictValue: '2', dictLabel: '最近30天'},
        {dictValue: '3', dictLabel: '最近90天'},
      ],
      loading: false,
      tableList: [],
      total: 0,
      pageSizeList: [10, 20, 30, 50],
      dialogCreatedVisible: false,
      ruleForm: {},
      rules: {},
      ruleFormList: [],
      riskTypeList: [],

      editVisible: false,
      editForm: {},
      editRules: {},
      editLoading: false,
    }
  },
  watch: {},
  methods: {
    // 查询
    handleQuery() {
      this.queryForm.pageNum = 1
      this.getListFn()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.getListFn()
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.getListFn()
    },
    // 重置
    handleReset() {
      this.queryForm = {
        weUserIdList: [],
        startTime: '',
        endTime: '',
        timeType: '0',
        ruleIdList: [],
        pageNum: 1,
        pageSize: 10,
      }
      this.radioDateSelect('0')
    },
    getListFn() {
      console.log('this.queryForm::::::::', this.queryForm)
      let obj = {
        weUserIdList: this.queryForm.weUserIdList,
        startTime: this.queryForm.dateRange?.[0],
        endTime: this.queryForm.dateRange?.[1],
        ruleIdList: this.queryForm.ruleIdList,
        pageNum: this.queryForm.pageNum,
        pageSize: this.queryForm.pageSize,
      }
      this.tableList = []
      this.loading = true
      $api.getRiskRecordList(obj).then(res => {
        if (res.code == 200) {
          this.tableList = res.rows
          this.total = res.total
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 日期单选框
    radioDateSelect(val) {
      let da = 0
      if (val == '0') {
        da = 0
      } else if (val == '1') {
        da = 7
      } else if (val == '2') {
        da = 30
      } else if (val == '3') {
        da = 90
      }
      let currentDate = new Date();
      let nowDate = new Date()
      nowDate.setHours(23, 59, 59, 59)
      let endDate = currentDate.toISOString().substring(0, 10)
      currentDate.setHours(0, 0, 0, 0); // 将时间设置为当天的凌晨
      // let endDate = currentDate.toISOString().substring(0, 10); // 当天的日期作为结束日期
      currentDate.setDate(currentDate.getDate() - da + 1); // 将日期设置为需要的天数
      let startDate = currentDate.toISOString().substring(0, 10); // 将设置的天数作为开始日期
      let datesArray = [startDate, endDate]
      if (da != 0) {
        this.queryForm.dateRange = datesArray
        this.$set(this.queryForm, 'dateRange', datesArray)
      } else {
        this.queryForm.dateRange = []
        this.$set(this.queryForm, 'dateRange', [])
      }
      this.getListFn()
    },
    // 编辑
    editItem(row) {
      console.log('row::::::', row)
      this.editForm = {
        weUserName: row.weUserName,
        ruleId: row.ruleId,
        label: this.riskTypeList.filter(item => item.dictValue == row.ruleId)[0].dictLabel,
        riskId: row.riskId,
        ruleLimit: row.ruleLimit,
      }
      this.editRules = {
        ruleLimit: {required: true, message: `${this.editForm.label}不能为空`, trigger: 'blur'}
      }
      this.editVisible = true
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
      })
    },
    // 风控数据编辑
    editSubmit() {
      let obj = {
        riskId: this.editForm.riskId,
        ruleList: [{
          ruleId: this.editForm.ruleId,
          ruleLimit: this.editForm.ruleLimit
        }]
      }
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.editLoading = true
          $api.editRiskConfig(obj).then(res => {
            if (res.code == 200) {
              this.$message.success(' 编辑成功')
              this.editVisible = false
              this.getListFn()
            }
            this.editLoading = false
          }).catch(() => {
            this.editLoading = false
          })
        }
      })
    }
  },
  mounted() {
    this.getDicts('risk_type').then((res) => {
      if (res.code === 200) {
        this.riskTypeList = res.data
        this.enumOpt.ruleList = res.data
      }
    })
    api.getList({
      name: '',
      department: '',
      pageNum: 1,
      pageSize: 99999,
      isActivate: '',
    })
      .then(({rows}) => {
        const data = rows.map(item => {
          return {
            dictLabel: item.name,
            dictValue: item.userId,
          }
        })
        this.enumOpt.weUserIdList = JSON.parse(JSON.stringify(data))
      })

    this.handleQuery()
  },
  created() {
  },
}

</script>

<style lang="scss" scoped>

.app-container {
  height: 100%;
}

.header-search {
  display: flex;
  align-items: center;
}

::v-deep {
  .el-radio-button__orig-radio:checked + .el-radio-button__inner {
    color: #082AFF;
    border-color: #082AFF;
    background: transparent;
    -webkit-box-shadow: -1px 0 0 0 #082AFF;
    box-shadow: -1px 0 0 0 #082AFF;
  }

  .el-input-number.is-controls-right .el-input__inner {
    text-align: left;
  }
}

.dialog-ruleForm-info {
  font-size: 12px;
  margin: 12px 0 0 32px;
  color: #999999;
}

.header-search {
  display: flex;
  align-items: center;
}
</style>
